package com.af.community.smart.business

import android.content.Intent
import android.os.Bundle
import com.af.community.smart.base.BaseActivity
import com.af.community.smart.base.oneClick
import com.af.community.smart.fragment.PeopleFirstFragment
import com.af.community.smart.fragment.PeopleSecondFragment
import com.af.community.smart.model.PeopleData
import com.af.community.smart.utils.ActivityStack
import com.af.community.smart.utils.OSSHelper
import com.af.community.smart.utils.observe
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lxj.matisse.Matisse
import org.jetbrains.anko.toast

class PatrolPeopleAddActivity : BaseActivity() {

    private lateinit var peopleType: String
    private lateinit var peopleData: PeopleData
    private lateinit var firstFragment: PeopleFirstFragment
    private lateinit var secondFragment: PeopleSecondFragment

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_patrol_people_add)
        initTitle("添加人口", "保存")

        observe("people_check") {
            ActivityStack.screenManager.popActivities(
                this@PatrolPeopleAddActivity::class.java,
                PatrolPeopleSelectActivity::class.java,
                PatrolPeopleScanActivity::class.java
            )
        }
    }

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        peopleType = intent.getStringExtra("type")
        peopleData = intent.getSerializableExtra("data") as PeopleData

        when (peopleType) {
            "1" -> {
                firstFragment = PeopleFirstFragment().apply {
                    arguments = Bundle().apply { putSerializable("data", peopleData) }
                }

                supportFragmentManager.beginTransaction()
                    .add(R.id.patrol_container, firstFragment)
                    .commit()
            }
            "2" -> {
                secondFragment = PeopleSecondFragment().apply {
                    arguments = Bundle().apply { putSerializable("data", peopleData) }
                }

                supportFragmentManager.beginTransaction()
                    .add(R.id.patrol_container, secondFragment)
                    .commit()
            }
        }

        tvRight?.oneClick {
            when (peopleType) {
                "1" -> firstFragment.getAddData()
                "2" -> secondFragment.getAddData()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                42 -> {
                    val imgSelect = ArrayList<String>()
                    val imgList = Matisse.obtainSelectPathResult(data)
                    val imgCapture = Matisse.obtainCaptureImageResult(data)

                    when {
                        !imgList.isNullOrEmpty() -> imgSelect.addAll(imgList)
                        imgCapture != null -> imgSelect.add(imgCapture)
                    }

                    if (imgSelect.isNotEmpty()) {
                        OSSHelper.manager.uploadImages(
                            pathList = imgSelect,
                            onBefore = { showLoading() },
                            onAfter = { dismissLoading() },
                            onSuccess = { _, result ->
                                if (result != null) {
                                    val img = result.responseHeader["url"].orEmpty()
                                    if (img.isNotEmpty()) {
                                        LiveEventBus.get("people_prove").post(img)
                                    }
                                }
                            },
                            OnError = { index, _ -> if (index < 0) toast("网络异常，请稍候再试！") }
                        )
                    }
                }
                52 -> {
                    val imgList = Matisse.obtainSelectPathResult(data)
                    val imgCapture = Matisse.obtainCaptureImageResult(data)
                    val imgSelect = when {
                        !imgList.isNullOrEmpty() -> imgList[0]
                        imgCapture != null -> imgCapture
                        else -> ""
                    }
                    if (imgSelect.isNotEmpty()) {
                        OSSHelper.manager.uploadImage(
                            path = imgSelect,
                            onBefore = { showLoading() },
                            onAfter = { dismissLoading() },
                            onSuccess = {
                                val img = it.responseHeader["url"].orEmpty()
                                if (img.isNotEmpty()) {
                                    LiveEventBus.get("people_avatar").post(img)
                                }
                            },
                            OnError = { toast("网络异常，请稍候再试！") }
                        )
                    }
                }
            }
        }
    }

}
